package com.parking.bang.rpc.serialize.kryo;

import org.objenesis.strategy.StdInstantiatorStrategy;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
import com.parking.bang.rpc.model.ReqMessage;
import com.parking.bang.rpc.model.RespMessage;

/**
 * Pool Factory + single
 * 
 * @author ivan
 *
 */
public class KryoPoolFactory {
	private static KryoPoolFactory poolFactory = null;

	private KryoPoolFactory() {
	}

	private KryoFactory factory = new KryoFactory() {
		public Kryo create() {
			Kryo kryo = new Kryo();
			kryo.setReferences(false);
			kryo.register(ReqMessage.class);
			kryo.register(RespMessage.class);
			kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
			return kryo;
		}
	};

	private KryoPool pool = new KryoPool.Builder(factory).build();

	public static KryoPool getKryoPoolInstance() {
		if (poolFactory == null) {
			synchronized (KryoPoolFactory.class) {
				if (poolFactory == null) {
					poolFactory = new KryoPoolFactory();
				}
			}
		}
		return poolFactory.getPool();
	}

	public KryoPool getPool() {
		return pool;
	}
}
